package com.emapp.lccode.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.chad.library.adapter.base.BaseQuickAdapter
import com.emapp.lccode.R
import com.emapp.lccode.base.BaseConfig
import com.emapp.lccode.bean.DeviceKt
import com.emapp.lccode.databinding.ListitemProgressBinding
import com.emapp.lccode.utils.clickNoRepeat

class ProgressAdapter(private val listener: OnButtonClickListener) :
    BaseQuickAdapter<DeviceKt, ProgressAdapter.VH>() {
    var build_status = ""

    class VH(
        parent: ViewGroup,
        val binding: ListitemProgressBinding = ListitemProgressBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        ),
    ) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH {
        return VH(parent)
    }

    override fun onBindViewHolder(holder: VH, position: Int, item: DeviceKt?) {
        if (item == null) return
        holder.binding.tvName.text = item.name
        holder.binding.tvDate.text = item.date_time
        holder.binding.ivLine1.visibility = if (position == 0) View.INVISIBLE else View.VISIBLE
        if (item.fileArray != null && item.fileArray.isNotEmpty()) {
            Glide.with(context)
                .load(BaseConfig.ROOT_URL + item.fileArray[0].filepath)
                .centerCrop()
                .placeholder(R.color.app_color_f6)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(holder.binding.ivImage)
        }
        holder.binding.tvSee.clickNoRepeat {
            listener.onSee(position)
        }
        holder.binding.tvEdit.clickNoRepeat {
            listener.onEdit(position)
        }
        if (build_status == "3") {
            holder.binding.tvEdit.visibility = View.GONE
        }
    }

    interface OnButtonClickListener {
        fun onSee(position: Int)
        fun onEdit(position: Int)
    }

}